<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<a id="out2csv"  href="#" >导出csv</a>

	<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
	<script type="text/javascript">
	/*
参数1 data对象
 data导入的数据类型 一个数组对象
 heads头部名称
 columns要输出的字段名
 remove_targets是否需要过滤html标签
 spe_column需要处理的数字字段 会在之前加`
 例子：
{
 'data':[{id:1,name:'a',sort:10},{id:2,name:'b',sort:9},{id:3,name:'c',sort:8}],
 'heads':['id号','姓名'],
 'columns':['id','name'],
 'remove_targets':false,
 'spe_column':['id'],
}
参数2 fun获取csv内容后的回调函数
*/
var data2csv = function(data = {},fun = function(str){return str}){
 
     //验证并处理参数
     if(data.heads.length != data.columns.length){
         return {'code':false,'msg':'表头和内容字段数不符'};
     }
 
     if(!data.remove_targets){
         data.remove_targets = false;
     }else{
         data.remove_targets = true;
     }
 
     csv_data = data.data;
     heads = data.heads;
     columns = data.columns;
     spe_column = data.spe_column;
     remove_targets = data.remove_targets;
 
     // 构造csv内容
     var str = "";
     for(var j in csv_data){
         e = csv_data[j];
         for(var i in columns){
            c = e[columns[i]];
            if(remove_targets){
                c = c.replace(/<[^>]+>/g,"")+",";
            } 
            if(spe_column.length>0 && spe_column.indexOf(columns[i])>-1){
                // c = '`'+c;//表格前添加自定义内容
            }
        str += c;
        }
    str += "\n";
    }
    head_str = "";
    for(var i in heads){
        head_str += heads[i]+",";
    }
    head_str += "\n";
    str = head_str + str;
    return fun(str);
}

//生成并下载文件
function download_file(file_name, content, o = true) {
   if(o){
        content = '\ufeff'+content
   }
   var aTag = document.createElement('a');
   var blob = new Blob([content]);
   aTag.download = file_name;
   aTag.href = URL.createObjectURL(blob);
   aTag.click();
   URL.revokeObjectURL(blob);
}
var csv = [
{
	'io':'0',
     'num':'10',
     'em_name':'0',
     'money':'0',
     'dismoney':'0',
     'discount':'0',
    'tmoney':'0',
    'pay_cate':'0',
    'etime':'0',
    'table_name':'0',
    'main_dismoney':'0',
    'status':'0',
    'user':'0',
    'base':'0',
}
];
var head = [
     '交易类型',
     '订单号',
     '操作人姓名及编号',
     '原价总额',
     '优惠金额',
     '优惠比率',
     '实付金额',
     '支付方式',
     '结算时间',
     '餐桌号',
     '整单优惠',
     '状态',
     '会员信息',
     '备注信息',
];
var column = [
     'io',
     'num',
     'em_name',
     'money',
     'dismoney',
     'discount',
    'tmoney',
    'pay_cate',
    'etime',
    'table_name',
    'main_dismoney',
    'status',
    'user',
    'base',
];
var num_column = ['num'];
data2csv({
    'data':csv,
    'heads':head,
    'columns':column,
    'spe_column':num_column,
    'remove_targets':true,
},function(content){
    console.log(content);
    // download_file('order_list.csv',content,true)
    $('#out2csv').on('click',function(){
    	download_file('order_list.csv',content,true)
    })
});
	</script>
</body>
</html>